package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wmz
 * @date 2024/1/25 22:19
 * @description SetmealServiceImpl
 */
@Service("setmealService")
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增菜品
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        //获取套餐
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.save(setmeal);
        //获取套餐的主键值
        Long setmealId = setmeal.getId();
        //获取菜品
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        if(setmealDishList != null && setmealDishList.size() > 0) {
            setmealDishList.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
        }
        //保存套餐和菜品的关联关系
        setmealDishMapper.insert(setmealDishList);
    }
    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmealVOList = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(setmealVOList.getTotal(),setmealVOList.getResult());
    }

    /**
     * 根据ids删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteSetmealWithSetmealDishByIds(List<Long> ids) {
        List<Setmeal> setmealList = setmealMapper.selectSetmealByIds(ids);
        if(setmealList != null && setmealList.size() > 0) {
            //判断套餐是否处于起售状态
            setmealList.forEach(setmeal -> {
                if(setmeal.getStatus() == StatusConstant.ENABLE) {
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
                }
            });
            //删除套餐
            setmealMapper.deleteSetmealByIds(ids);
        }
        //删除套餐和菜品对应关系
        setmealDishMapper.deleteSetmealDishBySetmealIds(ids);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO querySetmealById(Long id) {
        //查询套餐
        SetmealVO setmealVO = setmealMapper.selectSetmealById(id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectSetmealDishBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }

    /**
     * 更新套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //1.更新setmeal表
        setmealMapper.update(setmeal);

        //2.更新setmeal_dish表
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        if(setmealDishList != null && setmealDishList.size()>0) {
            List<Long> setmealIdList = new ArrayList<>();
            //获取套餐id
            setmealIdList.add(setmeal.getId());
            //给新插入的套餐菜品加入套餐id
            setmealDishList.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
            //2.1先删除之前的关联信息
            setmealDishMapper.deleteSetmealDishBySetmealIds(setmealIdList);
            //2.2新增关联信息
            setmealDishMapper.insert(setmealDishList);
        }

    }
    /**
     * 套餐起售/停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //进行分表查询-此处也可进行表连接查询

        //根据setmealId查询dishId
        List<Long> dishIdList = setmealDishMapper.selectDishIdBySetmealId(id);
        if(dishIdList != null && dishIdList.size()>0) {
            //根据dishIdList查询菜品
            List<Dish> dishList =dishMapper.selectByIds(dishIdList);
            if(dishList!=null && dishList.size()>0) {
                //套餐内不能包含未起售的菜品
                dishList.forEach(dish -> {
                    if(dish.getStatus() == StatusConstant.DISABLE){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }

       //进行套餐起售/停售
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        return setmealMapper.list(setmeal);
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
